﻿using System;
using System.Collections.Generic;
using System.Text;
using YouHP.Model;
using YouHP.BLL;
using YouHP.Utility;

namespace YouHP.Pages.Member
{
   public class PmSentbox:BasePage
    { 
    	protected List<MessageInfo> PmList = new List<MessageInfo>();
        protected int UsedMsgCount;
        protected int MaxMsg = 20;
    
        protected int usedmsgbarwidth;
        protected int unusedmsgbarwidth;

        private int _PmCount;
        protected int PmCount {get {return _PmCount;}}
        protected int PageSize=15 ;
        protected int PageCount ;
        protected int PageIndex ;
        protected string  PageNumbers ;

        protected override void ShowPage()
        {
            base.ShowPage();

            if (IsPost) HandlePost();
            else HandleGet();

            UsedMsgCount = PmCount;
            usedmsgbarwidth = UsedMsgCount * 100 / MaxMsg;
            unusedmsgbarwidth = 100 - usedmsgbarwidth;

            

        }

        public void HandlePost()
        {
            string[] Pmitemid = Utils.SplitString(RequestTools.GetFormString("pmitemid"), ",");
            bool succ = true;
            List<int> pmids = new List<int>();
            if (succ)
            {
                if (Pmitemid.Length <= 0) succ = false;
                else if (Pmitemid.Length > PageSize) succ = false;
            }
          
            if (succ)
            {
                foreach (string str in Pmitemid)
                {
                    int temp=Utils.StrToInt(str, -1);
                    if (temp <= 0)
                    {
                        succ = false;
                        break;

                    }
                    pmids.Add(temp);

                }
            }

        
            if (succ)
            {
                MessageBLL.Instance.DeleteMessage(pmids.ToArray());
                this.SetUrl("PmSentbox.aspx");
                this.SetMetaRefresh(2, "PmSentbox.aspx");
                this.AddMsgLine("删除成功");
            }
            else
            {
                this.AddErrLine("删除失败");
                SetShowBackLink(true);
            }


        }
        
        public void HandleGet()
        {
            
            PageIndex = RequestTools.GetQueryInt("page", 1);
        	PageIndex=Math.Min(PageIndex,PageCount);
            PmList = MessageBLL.Instance.GetMessageList(PageSize, PageIndex, UserData.Id, 1, -1, out _PmCount);
            PageCount = Utils.GetPageCount(PmCount, PageSize);
        	PageNumbers = Utils.GetPageNumbers(PageIndex, PageCount, "PmSentbox.aspx", 8,"page",null);
        }
        
    }
}
